<?php

/**
 * uvmcSolrDoctrinePager class 
 *
 * @package default
 * @author Marc
 **/

class uvmcSolrDoctrinePager extends sfDoctrinePager
{
  protected
    $dispatcher = null,
    $primaryKey = 'id',
    $solrQuery = null,
    $requestParameters = null;
    
  
  public function __construct($class, $dispatcher, $requestParameters, $maxPerPage = 10)
  {
    $this->setDispatcher($dispatcher);
    $this->setRequestParameters($requestParameters);
    
    parent::__construct($class, $maxPerPage);
  }
  
  public function askSolr()
  {
    $event = $this->dispatcher->notify(new sfEvent($this,
                                                   'uvmc_solr.search',
                                                   array('query' => $this->getSolrQuery(),
                                                         'params' => $this->getRequestParameters()->getParameterHolder())));
    
    $this->rawResponse = unserialize($event->getReturnValue()->getRawResponse());
    $this->rawDocs = $this->rawResponse['response']['docs'];
    $this->rawHighlighting = $this->rawResponse['highlighting'];
  }
  
  /**
   * Initialize the pager instance and prepare it to be used for rendering
   */
  public function init()
  {
    $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
    
    $this->getRequestParameters()->setParameter('start', $offset);
    $this->getRequestParameters()->setParameter('rows'  , $this->getMaxPerPage());
    
    $this->askSolr();
    $ids = $this->extractPrimaryKeys();
    $q = Doctrine::getTable($this->getClass())->getBaseQueryForSolrResults($this->getPrimaryKey(), $ids);
    $this->setQuery($q);
    
    $this->setNbResults($this->rawResponse['response']['numFound']);
    $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
  }
  
  protected function extractPrimaryKeys()
  {
    $uniqueIds = array();
    foreach ($this->rawDocs as $doc)
    {
      $uniqueIds[] = $doc[$this->getPrimaryKey()];
    }
    
    return $uniqueIds;
  }
  
  public function getRawResponse()
  {
    return $this->rawResponse;
  }
  
  /**
   * Returns the facets from a response
   */
  public function getResultsFacets()
  {
    return $this->rawResponse['facet_counts'];
  }
  
  /**
   * @return array
   */
  public function getRawHighlighting()
  {
    return $this->rawHighlighting;
  }
  
  /**
   * @param sfEventDispatcher $dispatcher
   */
  public function setDispatcher(sfEventDispatcher $dispatcher)
  {
    $this->dispatcher = $dispatcher;
  }
  
  /**
   * @return sfEventDispatcher
   */
  public function getDispatcher()
  {
    $this->dispatcher = $dispatcher;
  }
  
  /*
   * @param uvmc uvmcSolrRequestParameters
   */
  public function setRequestParameters($parameters)
  {
    $this->requestParameters = $parameters;
  }
  
  /**
   * @return uvmcSolrRequestParameters parameters
   */
  public function getRequestParameters()
  {
    return $this->requestParameters;
  }
  
  /**
   * @param string $name
   */
  public function setModelUniqueIdColumn($name)
  {
    $this->modelUniqueIdColumn = $name;
  }
  
  /**
   * @return string model's unique id column correspondance 
   */
  public function getModelUniqueIdColumn()
  {
    return $this->modelUniqueIdColumn;
  }
  
  /**
   * @param $q string Solr query
   */
  public function setSolrQuery($q)
  {
    $this->solrQuery = $q;
  }  
  
  /**
   * @return string 
   */
  public function getSolrQuery()
  {
    return $this->solrQuery;
  }
  
  /**
   * Return the highlight identified by $key
   * @param $key string
   * @return string highlight or empty
   */
  public function getHighlight($key)
  {
    if (!array_key_exists($key, $this->rawHighlighting))
    {
      return '';
    }
    $hls = array_values($this->rawHighlighting[$key]);
    
    return isset($hls[0][0]) ? strip_tags($hls[0][0], '<em>') : '';
  }
  
  /**
   * set Solr unique key name
   */
  public function setPrimaryKey($name)
  {
    $this->primaryKey = $name;
  }
  
  /**
   * get Solr uniquekey name
   */
  public function getPrimaryKey()
  {
    return $this->primaryKey;
  }
}